package com.david.zhufengfm;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.david.zhufengfm.adapters.AlbumsDetailTracksAdapter;
import com.david.zhufengfm.entity.album.AlbumDetail;
import com.david.zhufengfm.entity.album.AlbumDetailInfo;
import com.david.zhufengfm.entity.album.AlbumTrackDetail;
import com.david.zhufengfm.entity.album.PlayTrack;
import com.david.zhufengfm.entity.discoverrecommend.AlbumInfo;
import com.david.zhufengfm.service.MediaService;
import com.david.zhufengfm.tasks.AlbumDetailTask;
import com.david.zhufengfm.tasks.PlayTracksTask;
import com.david.zhufengfm.tasks.TaskCallback;
import com.david.zhufengfm.tasks.TaskResult;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AlbumDetailActivity extends AppCompatActivity implements TaskCallback {

    private long albumId;
    private long trackId;
    private List<AlbumTrackDetail>tracks;
    private AlbumDetailInfo albumDetailInfo=null;
    private AlbumsDetailTracksAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_detail);
        albumId=getIntent().getLongExtra("albumId",0);
        trackId=getIntent().getLongExtra("trackId",0);
        AlbumDetailTask task=new AlbumDetailTask(this);
        PlayTracksTask playTask=new PlayTracksTask(this);
        tracks=new LinkedList<>();
        task.execute(""+albumId);
        playTask.execute(""+albumId,""+trackId);
        ListView listView= (ListView) findViewById(R.id.lv_album_detail_list);
        adapter = new AlbumsDetailTracksAdapter(tracks,this);
        listView.setAdapter(adapter);

    }

    private void initData() {
        TextView tvAlbumTitle= (TextView) findViewById(R.id.tv_album_detail_title);
        TextView tvAlbumSubTitle= (TextView) findViewById(R.id.tv_album_detail_play_subTitle);
        TextView tvPlayTitle= (TextView) findViewById(R.id.tv_album_detail_play_title);
        ImageView ivAlbumIcon= (ImageView) findViewById(R.id.iv_album_detail_head_icon);
        ImageView ivAlbumLogo= (ImageView) findViewById(R.id.iv_album_detail_head_title_icon);

        if (albumDetailInfo != null) {
            tvAlbumTitle.setText(albumDetailInfo.getTitle());
            tvPlayTitle.setText(albumDetailInfo.getTitle());
            tvAlbumSubTitle.setText(albumDetailInfo.getIntro());
            String logoUrl=albumDetailInfo.getAvatarPath();
            String iconUrl=albumDetailInfo.getCoverSmall();
            Picasso.with(this).load(iconUrl).into(ivAlbumIcon);
            Picasso.with(this).load(logoUrl).into(ivAlbumLogo);
        }
    }

    @Override
    public void onTaskFinished(TaskResult result) {

        if (result != null) {
            if (result.action == Constants.TASK_ACTION_ALBUM_DETAIL) {
                if (result.resultCode == Constants.TASK_RESULT_OK) {
                    Object data = result.data;
                    if (data != null&&data instanceof AlbumDetail) {
                        AlbumDetail albumDetail= (AlbumDetail) data;
                        albumDetailInfo=albumDetail.getAlbumInfo();
                        initData();
                        tracks.addAll(albumDetail.getTracks());
                        Intent serviceIntent=new Intent();
                        adapter.notifyDataSetChanged();

                    }

                }
            }else if(result.action==Constants.TAST_PLAY_TRACKS){
                if (result.resultCode == Constants.TASK_RESULT_OK) {
                    Object data = result.data;
                    if(data!=null&&data instanceof List){
                        ArrayList<PlayTrack>playTracks= (ArrayList<PlayTrack>) data;
                        Log.i("playTracks",""+playTracks.size());
//                        PlayTrack[]tracks=new PlayTrack[playTracks.size()];
//                        PlayTrack[] tracksArr = playTracks.toArray(tracks);
                        Intent serviceIntent=new Intent(AlbumDetailActivity.this,MediaService.class);

                        serviceIntent.putExtra("tracks",playTracks);

                        serviceIntent.putExtra("currentTrackId", trackId);
//                        serviceIntent.putParcelableArrayListExtra()
                        Toast.makeText(AlbumDetailActivity.this, "即将播放", Toast.LENGTH_SHORT).show();
                       startService(serviceIntent);
                    }

                }
            }
        }
    }
}
